  // pages/collect/collect.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _num: 1,
    display_content:"none",
    hide_add:"block",
    hide_delete:"none",
    add_margin:0,
    isFalse:false,
    isAllSelect: false,
    display_ware:"block",
    delete:true,
    selection:true,
    // 商品详情介绍
    carts: [
      // {
      //   pic: "../image/order_list_01_03.png",
      //   name: "2018年最新村“两委”换届啊空间风格较大付款党换届啊空间风格较换届啊空间风格较大付款党",
      //   price: 89.90,
      //   isSelect: false,
      //   num: 1,
      //   // 数据设定
      //   count: {
      //     quantity: 1,
      //     min: 1,
      //     max: 20
      //   },
      // },
      // {
      //   pic: "../image/order_list_01_03.png",
      //   name: "2018年最新村“两委”换届啊空间风格较大付款党换届啊空间风格较换届啊空间风格较大付款党",
      //   price: 89.90,
      //   isSelect: false,
      //   num: 1,
      //   // 数据设定
      //   count: {
      //     quantity: 1,
      //     min: 1,
      //     max: 20
      //   },
      // },
      // {
      //   pic: "../image/order_list_01_03.png",
      //   name: "2018年最新村“两委”换届啊空间风格较大付款党换届啊空间风格较换届啊空间风格较大付款党",
      //   price: 89.90,
      //   isSelect: false,
      //   num: 1,
      //   // 数据设定
      //   count: {
      //     quantity: 1,
      //     min: 1,
      //     max: 20
      //   },
      // },
    ],
  },
  //勾选事件处理函数  
  switchSelect: function (e) {
    console.log(e)
    // 获取item项的id，和数组的下标值  
    var Allprice = 0, i = 0;
    let id = e.target.dataset.id,

      index = parseInt(e.target.dataset.index);
    this.data.carts[index].isSelect = !this.data.carts[index].isSelect;
    if (this.data.carts[index].isSelect){
      this.setData({
        isFalse:true,
      })
    }
    else{
      this.setData({
        isFalse: false,
      })
    }
    //是否全选判断
    for (i = 0; i < this.data.carts.length; i++) {
      Allprice = Allprice + this.data.carts[i].price * this.data.carts[i].num;
    }
    this.setData({
      carts: this.data.carts,
      isAllSelect: this.data.isAllSelect,
    })
  },
  //全选
  allSelect: function (e) {
    console.log(e)
    //处理全选逻辑\
    let that = this;
    var list = that.data.carts;
    var selection = that.data.selection;
    if (selection){
      list.forEach(function (data, i) {
        data.isSelect = true
      })
    }else{
      list.forEach(function (data, i) {
        data.isSelect = false
      })
    }
    that.setData({
      carts:list,
      selection: !selection
    })


    // let i = 0;
    // if (!this.data.isAllSelect) {
    //   for (i = 0; i < this.data.carts.length; i++) {
    //     if (this.data.carts[i].isSelect == false) {
    //       this.data.carts[i].isSelect = true;
    //     }
    //   }
    // }
    // else {
    //   for (i = 0; i < this.data.carts.length; i++) {
    //     this.data.carts[i].isSelect = false;
    //   }
    // }
    this.setData({
      //carts: this.data.carts,
      isAllSelect: !this.data.isAllSelect,
    })
  },

  //显示隐藏
  delete:function(){
    let that = this;
    var del = that.data.delete;
    if (del){
      that.setData({
        hide_add: "none",
        hide_delete: "block",
        add_margin: "80rpx",
        delete:false
      })
    }else{
      this.setData({
        hide_add: "block",
        hide_delete: "none",
        add_margin: "0",
        delete: true
      });
    }
    
  },
  conform:function(e){
    let that = this;
    var list = that.data.carts;         //数据
    var carid = '';
    list.forEach(function(data,i){
      if (data.isSelect){
        carid += data.colid+','
      }
    })
    if (carid == '' || carid == null){
      wx.showToast({
        title: '请选择删除商品',
        icon: 'success',
        duration: 1000
      })
      return false
    }else{
      wx.request({
        url: app.data.Url + 'collect/delecoll',
        data: {
          colid: carid,
          type: 0
        },
        method: 'get',
        success: function (res) {
          console.log(list)
          that.onLoad()
          // for(var i = 0;i<list.length;i++){
          //   if (list[i].isSelect){
          //     console.log(list[i].isSelect)
          //     list.splice(i,1)
          //     console.log(i+'删除后')
          //   }
          // }
          // list.forEach(function (data, i) {
          //   if (data.isSelect) {
          //     list.splice(i,1)
          //     console.log(i)
          //   }
          // })
          that.setData({
            carts:list
          })
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 1000
          })
        }
      })
    }
    this.setData({
      hide_add: "block",
      hide_delete: "none",
      add_margin: "0",
      delete:true
    });
  },

  //跳转详情
  onurl:function(e){
    if (e.currentTarget.dataset.type == 0){
      wx.showToast({
        title: '该商品已下架',
      })
    }else{
      wx.navigateTo({
        url: '../normal/normal?id=' + e.currentTarget.dataset.id,
      })
    }
    
    console.log(e.currentTarget.dataset.id)
  },
  focus_click: function (e) {
    this.setData({
      _num: e.target.dataset.num
    })
    if (e.target.dataset.num == 1){
      this.setData({
        display_ware:"block",
        display_content: "none",
      })
    };
    if(e.target.dataset.num == 2) {
      this.setData({
        display_ware:"none",
        display_content: "block",
      }) 
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let that=this
      wx.request({
        url: app.data.Url+'collect/collectlists',
        data:{
          // uid:wx.getStorageSync('userInfo').uid
          uid: wx.getStorageSync('userInfo').uid,
          type: 0
        },
        method:'get',
        success:function(res){
          console.log(res.data.data)
          that.setData({
            carts: res.data.data
          })
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  shouceng:function(e){
    wx.request({
      url: app.data.Url + 'cart/cartadd',
      data: {
        goodsid: e.currentTarget.id,
        num: 1,
        uid: wx.getStorageSync('userInfo').uid,
        type:0
      },
      method: 'post',
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: '添加成功！',
          })
        }
      }
    })
  }
})